package com.maze.game.view
{
	import com.maze.app.AppText;
	import com.maze.game.control.Alert;
	import com.maze.game.control.CoolButton;
	import com.maze.game.event.GameEvent;
	
	import flash.display.GradientType;
	import flash.display.SpreadMethod;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.filters.BevelFilter;
	import flash.filters.DropShadowFilter;
	import flash.geom.Matrix;
	
	public class MenuView extends Sprite
	{
		public function MenuView()
		{
			super();
			addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(evt:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			stage.addEventListener(Event.RESIZE, initMain);
			stage.dispatchEvent(new Event(Event.RESIZE));
		}
		
		private function initMain(evt:Event = null):void
		{
			if(stage.stageWidth>0)
				stage.removeEventListener(Event.RESIZE, initMain);
			else return;
			initContent();
		}
		
		private function playGame():void
		{
			dispatchEvent(new Event(GameEvent.PLAY_GAME));
		}
		
		private function mazeTest():void
		{
			dispatchEvent(new Event(GameEvent.MAZE_TEST));
		}
		
		private function initContent():void
		{
			var panel:Sprite = getBasePanel();
			addChild(panel);
			panel.x = stage.stageWidth/2 - panel.width/2;
			panel.y = stage.stageHeight/2 - panel.height/2;
			
			var btnWidth:uint = 500;
			var btnHeight:uint = 120;
			
			var spacey:uint = 20;
			var posy:uint = panel.height/2 - 2*btnHeight/2 - spacey;
			
			var maze1:CoolButton = new CoolButton(AppText.ALGORITHM_TEST,btnWidth,btnHeight);
			var maze1Click:Function;
			maze1.addEventListener(CoolButton.EVENT_CLICK, maze1Click = function(evt:Event):void{
				maze1.removeEventListener(CoolButton.EVENT_CLICK, maze1Click);
				mazeTest();
			});
			panel.addChild(maze1);
			maze1.x = panel.width/2 - maze1.width/2;
			maze1.y = posy;
			posy += maze1.height + spacey;
			
			var maze2:CoolButton = new CoolButton(AppText.PLAY,btnWidth,btnHeight);
			var maze2Click:Function;
			maze2.addEventListener(CoolButton.EVENT_CLICK, maze2Click = function(evt:Event):void{
				maze2.removeEventListener(CoolButton.EVENT_CLICK, maze2Click);
				playGame();
			});
			panel.addChild(maze2);
			maze2.x = panel.width/2 - maze2.width/2;
			maze2.y = posy;
			posy += maze2.height + spacey;
		}
		
		private function getBasePanel():Sprite
		{
			var panelWidth:uint = stage.stageWidth-100;
			var panelHeight:uint = stage.stageHeight-50;
			var base:Sprite = new Sprite();
			var colors:Array = [ 0xffffff, 0xC2C6CA, 0xa9b8c3];
			var alphas:Array = [ 1, 1, 1 ];
			var ratios:Array = [ 0x00, 0xB5, 0xFF ];
			var matrix:Matrix = new Matrix();
			var rounded:uint = 17;
			matrix.createGradientBox(panelWidth, panelHeight, (Math.PI / 2), 0, 0);
			base.graphics.beginGradientFill(GradientType.LINEAR, colors, alphas, ratios, matrix, SpreadMethod.PAD);
			base.graphics.drawRoundRect(0, 0, panelWidth, panelHeight, rounded, rounded);
			base.graphics.endFill();
			var filterB:BevelFilter = new BevelFilter(1, 60, 0xffffff, 1, 0x5c5c5c, .5, 1.1, 2.5, 4, 3, "inner", false);
			var filterD:DropShadowFilter = new DropShadowFilter(0, 60, 0x000000, .5, 60, 20, 1, 2, false, false, false);
			base.filters = [filterD,filterB];
			return base;
		}
	}
}